package com.itobase.statusDetect.Service.impl;

import com.itobase.statusDetect.Model.*;
import com.itobase.statusDetect.Repository.*;
import com.itobase.statusDetect.Service.TeachingService;
import com.itobase.statusDetect.Util.StringUtils;
import com.itobase.statusDetect.VO.TeachingVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author htyu
 * @date 2019/2/24
 */

@Service
public class TeachingServiceImpl implements TeachingService {

    @Autowired
    CourseRepository courseRepository;

    @Autowired
    UserRepository userRepository;

    @Autowired
    ClassroomRepository classroomRepository;

    @Autowired
    TimeRepository timeRepository;

    @Autowired
    TeachingRepository teachingRepository;

    public Teaching create(Teaching teaching){
        checkExist(teaching);
        return teachingRepository.save(teaching);
    }

    @Override
    public List<TeachingVO> getList(String keyword) {
        List<Teaching> list = teachingRepository.findAll();
        List<TeachingVO> list_vo = new ArrayList<>();
        List<TeachingVO> result;
        TeachingVO teachingVO;
        for(Teaching teaching: list){
            teachingVO = teachingConvertVO(teaching);
            list_vo.add(teachingVO);
        }
        if(StringUtils.isNotEmpty(keyword)){
            result = list_vo.stream().filter(teachingVO1 ->
                            teachingVO1.getName().contains(keyword)||
                            teachingVO1.getAddress().toLowerCase().contains(keyword.toLowerCase())||
                            teachingVO1.getTeacher().contains(keyword)||
                            teachingVO1.getSchoolTime().contains(keyword)||
                            teachingVO1.getSchoolYear().contains(keyword)||
                            teachingVO1.getSchoolWeek().contains(keyword))
                    .collect(Collectors.toList());
        }else{
            result = list_vo;
        }
        return result;
    }


    public TeachingVO teachingConvertVO(Teaching teaching){
        Course course = courseRepository.findById(teaching.getCourseID()).get();
        User teacher = userRepository.findById(teaching.getTeacherID()).get();
        Classroom classroom = classroomRepository.findById(teaching.getClassroomID()).get();
        Time time = timeRepository.findById(teaching.getTimeID()).get();
        TeachingVO teachingVO = new TeachingVO();
        teachingVO.setId(teaching.getId());
        teachingVO.setName(course.getName());
        teachingVO.setTeacher(teacher.getName());
        teachingVO.setAddress(classroom.getAddress());
        teachingVO.setSchoolTime(teaching.getSchoolTime());
        teachingVO.setNumber(teaching.getList().size());
        teachingVO.setSchoolWeek(teaching.getStartWeek()+"-"+teaching.getEndWeek());
        teachingVO.setSchoolYear(time.getSchoolYear()+time.getTerm());
        return teachingVO;
    }

    private void checkExist(Teaching teaching){
        List<Teaching> list = teachingRepository.findByCourseID(teaching.getCourseID());
        for(Teaching teaching_test : list){
            if(teaching_test.equals(teaching)){
                throw new InvalidParameterException("教学班已存在，请勿重复添加");
            }
        }
    }
}
